package com.zxmr.domain.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zxmr.CommonInit;
import com.zxmr.db.DBManager;
import com.zxmr.domain.Item;
import com.zxmr.domain.ItemPrice;
import com.zxmr.util.Utils;

public class ItemService {

	DBManager db = CommonInit.getDBManager();
	
	public boolean addItem(Item item)
	{
		try
		{
			ItemPrice price = new ItemPrice();
			price.setPrice(item.getPriceValue());
			price.setTime(Utils.getCurrentTime());
			price.setUser(item.getAddUser());
			
			db.getMapper().startTransaction();
				
			long priceId = (Long)db.getMapper().insert("item.addPrice", price);
			item.setPriceID(priceId);
			
			db.getMapper().insert("item.add", item);
			db.getMapper().commitTransaction();
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}
	public Item queryItem(int id)
	{
		try
		{
			Item item = (Item)db.getMapper().queryForObject("item.findByID", id);
			return item;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public Item queryItem(String name,int company)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		para.put("name", name);
		para.put("company", company);
		
		try
		{
			return (Item)db.getMapper().queryForObject("item.findByName", para);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Item> queryItems(int compay)
	{
		try
		{
			return (List<Item>)db.getMapper().queryForList("item.findByCompany", compay);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean updateItem(Item item)
	{
		try
		{
			// first get Item according to item id
			Item old = this.queryItem(item.getId());
			if(old == null)return false;
			float oldPrice = old.getPriceValue();
			
			// set all available update field
			old.setDescription(item.getDescription());
			old.setLastEditUser(item.getLastEditUser());
			old.setName(item.getName());
			old.setPriceValue(item.getPriceValue());
			
			// start transaction
			db.getMapper().startTransaction();
			// judge if need to update price
			if(oldPrice != old.getPriceValue())
			{
				ItemPrice newPrice = new ItemPrice();
				newPrice.setPrice(item.getPriceValue());
				newPrice.setTime(Utils.getCurrentTime());
				newPrice.setUser(item.getLastEditUser());
				long priceId = (Long)db.getMapper().insert("item.addPrice", newPrice);
				
				//update old price ids
				String oldPriceIDs = old.getHistoryPriceIDs();
				if(oldPriceIDs == null)oldPriceIDs = String.valueOf(old.getPriceID());
				else oldPriceIDs += ","+String.valueOf(old.getPriceID());
				
				old.setPriceID(priceId);
				old.setHistoryPriceIDs(oldPriceIDs);
			}
			db.getMapper().update("item.update", old);
			db.getMapper().commitTransaction();
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<ItemPrice> loadAllPrice(int itemId)
	{
		try
		{
			Item item = this.queryItem(itemId);
			if(item == null)return null;
			
			String ids = getPriceIdForItem(item);
			
			if(ids.equals(""))return null;
			
			System.out.println(ids);
			return (List<ItemPrice>)db.getMapper().queryForList("item.queryAllPrice", ids);
			
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public boolean deleteItem(Item item)
	{
		try
		{
			String ids = getPriceIdForItem(item);
			db.getMapper().startTransaction();
			db.getMapper().delete("item.delete", item.getId());
			if(ids!=null && !"".equals(ids))
				db.getMapper().delete("item.deletePrice",ids);
			db.getMapper().commitTransaction();
			
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return false;
	}
	private String getPriceIdForItem(Item item)
	{
		String ids = "";
		String oldPrice = item.getHistoryPriceIDs();
		long currentPriceId = item.getPriceID();
		if(oldPrice != null && !"".equals(oldPrice.trim()))
		{
			ids+=oldPrice;
		}
		
		if(currentPriceId > 0)
		{
			if(!ids.equals(""))ids += ",";
			ids+=String.valueOf(currentPriceId);
		}
		return ids;
	}
}
